package com.rms.utils;

import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 胡满意
 * @date 2019/10/31
 */
@Component
public class RedirectionAjax {
    /**
     * 判断是否为ajax请求
     *
     * @param request
     * @param response
     * @return
     */
    boolean isAjaxRequest(HttpServletRequest request, HttpServletResponse response) {
        try {
            //判断是否为ajax请求。
            if (request.getHeader("x-requested-with") != null && request.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    /**
     * ajax请求标记
     *
     * @param request
     * @param response
     * @param loginUrl
     */
    void ajaxHttpToLogin(HttpServletRequest request, HttpServletResponse response, String loginUrl) {
        try {
            response.setHeader("state", "error");
            response.setHeader("Path", request.getContextPath() +loginUrl);
            response.setStatus(HttpServletResponse.SC_FORBIDDEN);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
